package com.github.jiatao.floatview;

import android.app.Activity;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.Toast;

/**
 * Created on 2019/2/25 下午2:08
 *
 * @Author: jiatao
 * @Email: 535318720@qq.com
 * @Version: 1.0
 * @Description:
 */
public class FloatViewManger {

    private static FloatViewManger instance = null;//单例模式

    public static FloatViewManger getInstance(Activity context) {
        if (instance == null) {
            synchronized (FloatViewManger.class) {
                if (instance == null) {
                    instance = new FloatViewManger(context);
                }
            }
        }
        return instance;
    }

    private Activity mActivity;
    private FloatView floatView;
    private MenuViewPopupWindow popupWindow;

    public FloatViewManger(final Activity activity) {
        mActivity = activity;
        floatView = new FloatView(activity, R.drawable.logo,
                WindowManager.LayoutParams.TYPE_APPLICATION);
        popupWindow = new MenuViewPopupWindow(activity);

        initView();
    }

    private void initView() {

        /**
         * 设置菜单点击项
         */
        popupWindow.setMenuItemClick(new MenuViewPopupWindow.MenuItemClick() {
            @Override
            public void OnItemClick(int position) {
                switch (position) {
                    case 0:
                        popupWindow.Dismiss();
                        Toast.makeText(mActivity, "个人中心", Toast.LENGTH_SHORT).show();
                        break;
                    case 1:
                        popupWindow.Dismiss();
                        Toast.makeText(mActivity, "礼包", Toast.LENGTH_SHORT).show();
                        break;

                    default:
                        break;
                }
            }
        });


        /**
         * 菜单选项消失后，开启自动靠边
         */
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                floatView.timeToSide();
            }
        });

        /**
         * 点击浮标弹出菜单
         */
        floatView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (floatView.isLeft()) {
                    popupWindow.setMenuView(R.layout.float_left_menu_view)
                            .showAtLocation(v, Gravity.NO_GRAVITY, v.getLeft(), v.getTop());
                } else {
                    popupWindow.setMenuView(R.layout.float_right_menu_view).setViewAnimationStyle("").
                            showAtLocation(v, Gravity.NO_GRAVITY, v.getRight() - popupWindow.getWidth(), v.getTop());
                }
            }
        });
    }


    public void show() {
        floatView.show();
    }

    public void hide() {
        floatView.hide();
    }

    public void desroty() {
        floatView.release();
        floatView = null;

        instance = null;
    }
}
